//
//  MGProgressHUD.m
//  MiguC
//
//  Created by 赖锋海 on 2018/8/9.
//  Copyright © 2018年 咪咕动漫. All rights reserved.
//

#import "MGProgressHUD.h"

@implementation MGProgressHUD

+ (MBProgressHUD *)showHUDWithText:(NSString *)text addedTo:(UIView *)view {
    if (view == nil || [text isEqual:[NSNull null]] || text == nil || text.length == 0) {
        return nil;
    }
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.margin = 12.f;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = text;
    [MGProgressHUD defaultConfiguration:hud];
    [hud hideAnimated:YES afterDelay:2.f];
    return hud;
}

+ (void)showHUDWithText:(NSString *)text addedTo:(UIView *)view duration:(NSTimeInterval)duration completion:(MGProgressHUDBlock)completion {
    if (view == nil || [text isEqual:[NSNull null]] || text == nil || text.length == 0) {
        return;
    }
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.margin = 12.f;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = text;
    [MGProgressHUD defaultConfiguration:hud];
    [hud hideAnimated:YES afterDelay:duration];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (completion) {
            completion();
        }
    });
}

+ (MBProgressHUD *)showHUDActivityIndicatorViewAddedTo:(UIView *)view {
    if (view == nil) {
        return nil;
    }
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.margin = 20.f;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [MGProgressHUD defaultLoadingStyle];
    [MGProgressHUD defaultConfiguration:hud];
    hud.bezelView.backgroundColor = [UIColor clearColor];
    return hud;
}

+ (MBProgressHUD *)showHUDActivityIndicatorViewWithText:(NSString *)text addedTo:(UIView *)view {
    if (view == nil || [text isEqual:[NSNull null]] || text == nil || text.length == 0) {
        return nil;
    }
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.margin = 20.f;
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabel.text = text;
    hud.customView = [MGProgressHUD defaultLoadingStyle];
    [MGProgressHUD defaultActivityIndicatorConfiguration:hud];
    return hud;
}

+ (MBProgressHUD *)showHUDActivityIndicatorViewWithText:(NSString *)text addedTo:(UIView *)view duration:(NSTimeInterval)duration completion:(MGProgressHUDBlock)completion {
    if (view == nil || [text isEqual:[NSNull null]] || text == nil || text.length == 0) {
        return nil;
    }
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.margin = 20.f;
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabel.text = text;
    hud.customView = [MGProgressHUD defaultLoadingStyle];
    [MGProgressHUD defaultActivityIndicatorConfiguration:hud];
    [hud hideAnimated:YES afterDelay:duration];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (completion) {
            completion();
        }
    });
    return hud;
}

+ (void)hideHUDForView:(UIView *)view {
    if (view == nil) {
        return;
    }
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (void)defaultConfiguration:(MBProgressHUD *)hud {
    hud.detailsLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//    if (@available(iOS 13.0, *)) {
        hud.detailsLabel.textColor = [UIColor whiteColor];
        hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
//    } else {
//        hud.detailsLabel.textColor = [UIColor colorWithRed:113/255.f green:109/255.f blue:128/255.f alpha:1];
//        hud.bezelView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
//    }
    hud.bezelView.layer.cornerRadius = 8;
}

+ (void)defaultActivityIndicatorConfiguration:(MBProgressHUD *)hud {
    hud.detailsLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.detailsLabel.textColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    hud.bezelView.layer.cornerRadius = 6;
}

+ (UIImageView *)defaultLoadingStyle {
    UIImageView *imageView = [[UIImageView alloc] init];
    NSMutableArray *images = [NSMutableArray array];
    for (NSUInteger i = 0; i < 20; i++) {
        UIImage *image = [MGProgressHUD imageNamed:[NSString stringWithFormat:@"loading_refresh_%02zd", i] bundleNamed:@"MGLoader" class:[MGProgressHUD class]];
        [images addObject:image];
    }
    imageView.animationImages = images;
    imageView.animationDuration = images.count * 0.04;
    [imageView startAnimating];
    return imageView;
}

+ (UIImage *)imageNamed:(NSString *)imageName bundleNamed:(NSString *)bundleName class:(Class)aClass {
    NSBundle *bundle = [NSBundle bundleForClass:aClass];
    NSString *bundlePath = [bundle.resourcePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.bundle", bundleName]];
    NSBundle *resource_bundle = [NSBundle bundleWithPath:bundlePath];
    UIImage *image = [UIImage imageNamed:imageName inBundle:resource_bundle compatibleWithTraitCollection:nil];
    return image;
}

@end
